package com.example.administrator.test;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

public class RegisterActivity extends AppCompatActivity {

    private  MyApplication app;
    User user;

    @ViewInject(R.id.register_edit_account)
    private EditText account;
    @ViewInject(R.id.register_edit_password1)
    private EditText password1;
    @ViewInject(R.id.register_edit_password2)
    private EditText password2;
    @Event(value = {R.id.register_button2,R.id.login_button2,R.id.look})
    private void test(View view)
    {
        app = new MyApplication();
        DbManager db = x.getDb(app.getDaoConfig());
        switch (view.getId())
        {
            case R.id.register_button2:
                //账号密码不能为空
                if(account.getText().toString().equals("")||password1.getText().toString().equals("")||password2.getText().toString().equals("")){
                    Toast.makeText(RegisterActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                //两次密码要一致
                if(!password1.getText().toString().equals(password2.getText().toString())){
                    Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

//                //判断账号是否存在
                try {
                    user = db.selector(User.class).where("name","==",account.getText().toString()).findFirst();
                    if(user!=null){
                        Toast.makeText(RegisterActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }

                User user = new User();
                user.setName(account.getText().toString());
                user.setPassword(password1.getText().toString());
                try {
                    db.save(user);
                    account.setText("");
                    password1.setText("");
                    password2.setText("");
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                } catch (DbException e) {
                    e.printStackTrace();
                }

                break;

            case R.id.login_button2:
                Intent login = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(login);
                break;

            case R.id.look:

                try {
                    user = db.selector(User.class).findFirst();
                    if(user==null){
                        Toast.makeText(RegisterActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }

                try {
                    List<User> users = db.selector(User.class).findAll();
                    StringBuilder s = new StringBuilder();
                    if(users.size()!=0){
                        for (int i=0;i<users.size();i++){
                            s.append(users.get(i).toString()+"\n");
                        }
                        Toast.makeText(RegisterActivity.this, s+"", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(RegisterActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }

                break;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        x.view().inject(this);
        ActionBar bar = getSupportActionBar();
        if(bar!=null){
            bar.hide();
        }
    }
}
